package com.google.code.sant

trait Logger {
  import Logger._
  
  def log(message: String, throwable: Throwable, level: Logger.LogLevel)
  
  def log(message: String, level: Int) {
    log(message, null, level)
  }
  
  def verbose(message: String) {
    log(message, MSG_VERBOSE)
  }
  
  def verbose(message: String, throwable: Throwable) {
    log(message, throwable, MSG_VERBOSE)
  }
  
  def debug(message: String) {
    log(message, MSG_DEBUG)
  }
  
  def debug(message: String, throwable: Throwable) {
    log(message, throwable, MSG_DEBUG)
  }
  
  def info(message: String) {
    log(message, MSG_INFO)
  }
  
  def info(message: String, throwable: Throwable) {
    log(message, throwable, MSG_INFO)
  }
  
  def warn(message: String) {
    log(message, MSG_WARN)
  }
  
  def warn(message: String, throwable: Throwable) {
    log(message, throwable, MSG_WARN)
  }
  
  def error(message: String) {
    log(message, MSG_ERR)
  }
  
  def error(message: String, throwable: Throwable) {
    log(message, throwable, MSG_ERR)
  }
  
}

/**
 * Log all messages to project, otherwize to console
 */
trait ProjectLogger extends Logger {
	def log(message: String, throwable: Throwable, level: Logger.LogLevel) {
    Ant.projectOption.getOrElse(ConsoleLogger).log(message, throwable, level)
  }
}

object ConsoleLogger extends Logger {
  def log(message: String, throwable: Throwable, level: Logger.LogLevel) {
    Console.err.println(message)
    if (throwable ne null) throwable.printStackTrace()
  }
}

object Logger extends ProjectLogger {
  type LogLevel = Int
  
  val MSG_ERR = org.apache.tools.ant.Project.MSG_ERR
  val MSG_WARN = org.apache.tools.ant.Project.MSG_WARN
  val MSG_INFO = org.apache.tools.ant.Project.MSG_INFO
  val MSG_VERBOSE = org.apache.tools.ant.Project.MSG_VERBOSE
  val MSG_DEBUG = org.apache.tools.ant.Project.MSG_DEBUG
}